package cn.kgc.springbootdemo.config;

import cn.kgc.springbootdemo.aop.LimitRequestInterceptor;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * <h3>springbootdemo</h3>
 * <p>限流注解Bean 自动注入到Spring容器中</p>
 *
 * @author : DongZeJian
 * @date : 2020-05-26 15:55
 **/
@Configuration
@AllArgsConstructor
@ConditionalOnWebApplication
public class LimitRequestAutoConfig {

    @Bean
    public LimitRequestInterceptor limitInterceptor() {
        return new LimitRequestInterceptor();
    }

    @Bean
    public RedisTemplate<String, Serializable> limitRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
